package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Order;
import com.example.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/save")
    public String saveOrder(@RequestBody Order order) {
        // 验证必填项
        if (StringUtils.isBlank(order.getOrderNo())) {
            return "请填写工单编号";
        }
        if (StringUtils.isBlank(order.getTitle())) {
            return "请填写标题";
        }
        if (StringUtils.isBlank(order.getContent())) {
            return "请填写内容";
        }
        // 验证工单编号唯一性
        if (orderService.count(new LambdaQueryWrapper<Order>().eq(Order::getOrderNo,order.getOrderNo())) > 0) {
            return "工单编号已存在";
        }
        order.setCreateTime(LocalDateTime.now());
        if (orderService.save(order))
            return "工单保存成功";
        return "工单保存失败，请检查后重试";
    }

    @DeleteMapping("/delete/{id}")
    public String deleteOrder(@PathVariable Long id) {
        if (orderService.removeById(id))
            return "工单删除成功";
        return "工单删除失败";
    }

    @PostMapping("/update")
    public String updateOrder(@RequestBody Order order) {
        if (StringUtils.isBlank(order.getOrderNo())) {
            return "请填写工单编号";
        }
        if (StringUtils.isBlank(order.getTitle())) {
            return "请填写标题";
        }
        if (StringUtils.isBlank(order.getContent())) {
            return "请填写内容";
        }
        if (orderService.updateById(order))
            return "工单更新成功";
        return "工单更新失败";
    }

    //  访问路径 需要设置 page 和 size
    @GetMapping("/search")
    public IPage<Order> searchOrders(@RequestParam int page, @RequestParam int size, @RequestBody(required = false) Map<String, Object> params) {
        Page<Order> orderPage = new Page<>(page, size);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        if (params != null) {
            params.forEach(queryWrapper::eq);
        }
        return orderService.page(orderPage, queryWrapper);
    }

    @PostMapping("/fenpai")
    public String fenpaiOrder(@RequestParam Long orderId, @RequestParam Long handleDeptId) {
        Order order = orderService.getById(orderId);
        if (order == null) {
            return "工单不存在";
        }
        if (handleDeptId == null) {
            return "处理部门ID无效";
        }
        order.setHandleDeptId(handleDeptId);
        order.setFenpaiTime(LocalDateTime.now());
        if (orderService.updateById(order))
            return "工单分派成功";
        return "工单分派失败";
    }

    //    访问路径参数需要加开始日期 和 结束日期
    @GetMapping("/everyday")
    public List<Map<String, Object>> everyday(@RequestParam String startDate, @RequestParam String endDate) {
        LocalDate start = LocalDate.parse(startDate);
        LocalDate end = LocalDate.parse(endDate);
        return orderService.everyday(start, end);
    }

    @GetMapping("/everydept")
    public List<Map<String, Object>> everydept(@RequestParam String startDate, @RequestParam String endDate) {
        LocalDate start = LocalDate.parse(startDate);
        LocalDate end = LocalDate.parse(endDate);
        return orderService.everydept(start, end);
    }

    @GetMapping("/everytype")
    public List<Map<String, Object>> everytype(@RequestParam String startDate, @RequestParam String endDate) {
        LocalDate start = LocalDate.parse(startDate);
        LocalDate end = LocalDate.parse(endDate);
        return orderService.everytype(start, end);
    }
}

